import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class test6 {
    //力扣 18 四数之和
    public static void main(String[] args) {
        int[] nums = {1,-2,-5,-4,-3,3,3,5};
        int target = -11;
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> list = new ArrayList<>();

        for (int max1 = n - 1; max1 >= 3; ){
//               错误示范
//                if(nums[max1]<0){
//                    break;
//                }
            for (int max2 = max1 - 1; max2 >= 2; ){
//               错误示范
//                if(nums[max2]<0){
//                    break;
//                }
                int left = 0;
                int right = max2 - 1;
                long aim =(long) target-nums[max1]-nums[max2];
                while (left < right){
                    int sum = nums[left] + nums[right];
                    if (sum > aim) {
                        right--;
                    } else if (sum < aim) {
                        left++;
                    } else {
                        List<Integer> newlist = new ArrayList<>();
                        newlist.add(nums[left]);
                        newlist.add(nums[right]);
                        newlist.add(nums[max2]);
                        newlist.add(nums[max1]);
                        list.add(newlist);
//                    list.add(new ArrayList<Integer>(Arrays.asList(nums[left], nums[right], nums[max])));
                        left++; right--;
                        while (left < right && nums[left - 1] == nums[left]   ) {
                            left++;
                        }

                        while (left < right && nums[right + 1] == nums[right]  ) {
                            right--;
                        }
                    }
                }
                max2--;
                while (max2 >= 2 && nums[max2 + 1] == nums[max2]  ) {
                    max2--;
                }
            }
            max1--;
            while (max1 >= 3 && nums[max1 + 1] == nums[max1]  ) {
                max1--;
            }
        }





        for (int i1 = 0; i1 < list.size(); i1++) {

            for (int i2 = 0; i2 < list.get(i1).size(); i2++) {
                System.out.print(list.get(i1).get(i2) + " ");
            }

        }

    }
}
